package com.mapmymotion;

import java.util.HashMap;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;


public class FriendsAdapter extends SimpleAdapter {
	  Resources res;
private int[] colors = new int[] { 0x30FFFFFF, 0x3066FF66,0x30FFFF00,0x30FFFF33,0x30357EC7 };
JSONObject json_data;



public FriendsAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
    
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
	Toast.makeText(mainActivity.getContext(), "Notify Changed " , Toast.LENGTH_LONG).show();
	
	boolean _NotifyOnChange = true;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);

//  res = mainActivity.getContext().getResources();
//  int colorPos = position % colors.length;
  // Load the image and set it on the ImageView
 // ImageView imageView = (ImageView) view.findViewById(R.id.friendsdetail);

  TextView fullname = (TextView) view.findViewById(R.id.fullname);
  TextView sortnumber = (TextView) view.findViewById(R.id.sortnumber);
  TextView positionid = (TextView) view.findViewById(R.id.positionid);
  CharSequence pos = positionid.getText();
  ToggleButton addFriendButton=(ToggleButton) view.findViewById(R.id.acceptfriend);
  addFriendButton.setTag(pos);
  ToggleButton addInvite = (ToggleButton) view.findViewById(R.id.invitebutton);
  addInvite.setTag(pos);
  ToggleButton showFriendButton = (ToggleButton) view.findViewById(R.id.showfriendbutton);
  showFriendButton.setTag(pos);
  
 /// checkBox.setVisibility(View.GONE);
  addFriendButton.setVisibility(View.GONE);
  showFriendButton.setVisibility(View.GONE);
  addInvite.setVisibility(View.GONE);
  
  int li_positionid = Integer.parseInt((String) positionid.getText());
  int li_sortorder = Integer.parseInt((String) sortnumber.getText());

  if (li_positionid==-1){view.setBackgroundColor(colors[4]);
	fullname.setGravity(1);
	//fullname.setTextColor(colors[1]);
  } else {
	  fullname.setGravity(3);
	switch (li_sortorder) {

	case 3:  //Friend
		view.setBackgroundColor(colors[1]);
		showFriendButton.setVisibility(View.VISIBLE);
		break;
	case 1:  //Accept Friend
		view.setBackgroundColor(colors[2]);
		addFriendButton.setVisibility(View.VISIBLE);
		break;
	case 2:  //Awaiting Accept
		view.setBackgroundColor(colors[3]);
		break;
	case 0:  //Invite
		view.setBackgroundColor(colors[0]);
		addInvite.setVisibility(View.VISIBLE);
		break;
	}
}
/*	try {
		json_data = JWC.jArrayFriends.getJSONObject(position);
		// Contacts not invited yet
		if(json_data.getInt("sortorder")==3){
			view.setBackgroundColor(colors[1]);

//			imageView.setImageDrawable(res.getDrawable(R.drawable.friends));
//			imageView.setVisibility(View.VISIBLE);
			//REXELshowFriendButton.setVisibility(View.VISIBLE);

			}
		else
		// Accept Friends	
		if(json_data.getInt("sortorder")==1  ){
			view.setBackgroundColor(colors[2]);
			//imageView.setImageDrawable(res.getDrawable(R.drawable.headfriends));
			//REXELaddFriendButton.setVisibility(View.VISIBLE);	
		}
		// Awaiting Acceptance
		if(json_data.getInt("sortorder")==2  ){
			view.setBackgroundColor(colors[3]);
				
		}

		if(json_data.getInt("sortorder")==0  ){
		// Friends	
		view.setBackgroundColor(colors[0]);
		//REXELaddInvite.setVisibility(View.VISIBLE);
		}
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
*/
	
  /*view.setBackgroundColor(colors[colorPos]);

  // Load the image and set it on the ImageView
  ImageView imageView = (ImageView) view.findViewById(R.id.friendsdetail);
  if (colorPos==1){
	  imageView.setImageDrawable(res.getDrawable(R.drawable.friends));}
  else{
	  imageView.setImageDrawable(res.getDrawable(R.drawable.headfriends));} 
  
 */



  return view;
}

}